# coding=utf-8
# 防止中文乱码
import json
import redis

class OPRedis(object):

  def __init__(self):
    if not hasattr(OPRedis, 'pool'):
      OPRedis.getRedisCoon()  # 创建redis连接
    self.coon = redis.Redis(connection_pool=OPRedis.pool)

  @staticmethod
  def getRedisCoon():
    from common.utils.predefined import conf
    OPRedis.pool = redis.ConnectionPool(host=conf['redisconf']['host'], port=conf['redisconf']['port'], password=conf['redisconf']['password'], db=conf['redisconf']['db'])

  """
  string类型 {'key':'value'} redis操作
  """

  def setredis(self, key, value, time=None):
    # 非空即真非0即真
    if time:
      res = self.coon.setex(key, time, value)
    else:
      res = self.coon.set(key, value)
    return res

  def getRedis(self, key):
    res = self.coon.get(key)
    if res == None:
      return None
    return res.decode()

  def delRedis(self, key):
    res = self.coon.delete(key)
    return res

  """
  hash类型，{'name':{'key':'value'}} redis操作
  """

  def setHashRedis(self, name, key, value):
    res = self.coon.hset(name, key, value)
    return res

  def getHashRedis(self, name, key=None):
    # 判断key是否我为空，不为空，获取指定name内的某个key的value; 为空则获取name对应的所有value
    if key:
      res = self.coon.hget(name, key)
    else:
      res = self.coon.hgetall(name)
    return res

  def delHashRedis(self, name, key=None):
    if key:
      res = self.coon.hdel(name, key)
    else:
      res = self.coon.delete(name)
    return res
